<?php
namespace app\common\business;

use app\common\lib\ClassArr;
use app\common\lib\Num;
use app\common\lib\sms\AliSms;

class Sms
{
    public static function sendCode($phone_number, $len = 4, $type = 'ali')
    {
        if (empty($phone_number)) {
            return false;
        }

        $code = Num::getCode($len);

        //$res = AliSms::sendCode($phone_number, $code);

        //按照比例分配不同sms
        $typeArr = [
            'ali' => 100,
            'tcloud' => 0,
        ];

        $type = '';
        $typeSum = array_sum($typeArr);
        foreach ($typeArr as $key => $typeCur) {
            $randNum = mt_rand(1, $typeSum);             //抽取随机数
            if ($randNum <= $typeCur) {
                $type = $key;                         //得出结果
                break;
            } else {
                $typeSum -= $typeCur;
            }
        }

        $classStats = ClassArr::smsClessStat();
        $classObj = ClassArr::initClass($type, $classStats);
        $res = $classObj::sendCode($phone_number, $code);
        if ($res) {
            //存redis 过期时间1分钟
            cache(config('redis.code_pre').$phone_number, $code, config('redis.code_expire'));
        }
        return $res;
    }


}